/* GSManufactureDownServlet.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   np	2009-5-6	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import system.LoginContext;
import system.base.User;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.DefineDic;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.lang.StringUtils;

public class GSManufactureDownServlet extends HttpServlet {

    public void init(ServletConfig aConfig) throws ServletException {
        super.init(aConfig);
    }
    boolean isTel=false;//是否显示进价
    private ArrayList parse(HttpServletRequest request, HttpServletResponse response)  {
    	
        GSManufactureDAO tGSManufProcessor = new GSManufactureDAO();
        String sqlbuf = (String) request.getSession().getAttribute("querycond");
        String useTempTable = (String) request.getSession().getAttribute("useTempTable");
        String useUpperType = (String) request.getSession().getAttribute("useUpperType");
        GSManufactureProcessor processor = new GSManufactureProcessor();
        String sql="";   	
        String tableName="";
        if(useTempTable!=null&&!"".equals(useTempTable)){
			try {
				tableName = processor.initDataForArea(useUpperType);
				sql=sqlbuf.replaceAll(useTempTable, tableName);
			} catch (GSException e) {
				e.printStackTrace();
			}
        
        }else{
        	sql = sqlbuf;
        }
        ArrayList tTempList = null;
        try {
            tTempList = tGSManufProcessor.queryManufactureByOpt(sql);
            if(!"".equals(useTempTable)){
            	processor.dropTableForArea(tableName);
            }
        } catch (GSException e) {
        }
        
        
        return tTempList;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        response.setContentType("application/excel;charset=UTF-16");
        response.setHeader("Content-disposition", "attachment; filename=manufacture.xls");
        response.setHeader("Content-Description", "产品查询结果");
        ArrayList tList = parse(request, response);
        OutputStream tOutputStream = null;
        try {
        	LoginContext loginInfo =(LoginContext)request.getSession().getAttribute("LoginContext");
        	User user=loginInfo.getUser();
        	if(user!=null&&user.getTel()!=null&&user.getTel().equals("1")){
            	isTel=true;
            }else{
            	isTel=false;
            }
            tOutputStream = response.getOutputStream();
            writeExcel(tList, tOutputStream);
        } catch (Exception ex) {
            GSLogWriter.error(ex.getMessage());
        } finally {
            try {
                if (tOutputStream != null) {
                    tOutputStream.close();
                }
            } catch (IOException e) {
            }
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        doPost(request, response);
    }

    private void writeExcel(ArrayList aList, OutputStream tOutputStream) {
        try {
            HSSFWorkbook tWb = new HSSFWorkbook();
            HSSFSheet tSheet = tWb.createSheet();
            tWb.setSheetName(0, "产品浏览", HSSFCell.ENCODING_UTF_16);
            HSSFRow tRow = tSheet.createRow((short) 0);
            if (GSBaseConstants.PLANPRICE.equals("true")) {
                createCell(tWb, tRow, (short) 0, "种类编码", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 1, "集中编码", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 2, "产品名称", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 3, "价格(元)", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 4, "计划价(元)", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 5, "规格", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 6, "型号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 7, "质保期", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 8, "单位", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 9, "品牌", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 10, "生产厂商", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 11, "产地", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 12, "供应商", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 13, "图件号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 14, "结算模式", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 15, "是否正式产品", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 16, "是否采购", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 17, "所属仓库", HSSFCellStyle.ALIGN_CENTER);
                for (int i = 0; i < aList.size(); i++) {
                    tRow = tSheet.createRow((short) (i + 1));
                    GSManufactureBean mbean = (GSManufactureBean) aList.get(i);
                    createCell(tWb, tRow, (short) 0, mbean.getGs_manufacture_category_id());
                    createCell(tWb, tRow, (short) 1, mbean.getGs_concentricity());
                    createCell(tWb, tRow, (short) 2, mbean.getGs_manufacture_name());
                    createCell(tWb, tRow, (short) 3, StringUtils.format(mbean.getGs_manufacture_unitprice(), 4));
                    createCell(tWb, tRow, (short) 4, StringUtils.format(mbean.getPlan_price(), 2));
                    createCell(tWb, tRow, (short) 5, mbean.getGs_manufacture_spec());
                    createCell(tWb, tRow, (short) 6, mbean.getGs_manufacture_type());
                    createCell(tWb, tRow, (short) 7, mbean.getGs_quality_assure_period());
                    createCell(tWb, tRow, (short) 8, mbean.getGs_quant_unit());
                    createCell(tWb, tRow, (short) 9, mbean.getGs_manufacture_brand());
                    createCell(tWb, tRow, (short) 10, mbean.getGs_manufacturer_name());
                    createCell(tWb, tRow, (short) 11, mbean.getGs_make_area());
                    createCell(tWb, tRow, (short) 12, GSBaseManager.getProviderShortName(mbean.getGs_provider_id()));
                    createCell(tWb, tRow, (short) 13, mbean.getGs_drawing_peice_code());
                    createCell(tWb, tRow, (short) 14, DefineDic.getBalanceMode(mbean.getGs_balance_mode()));
                    createCell(tWb, tRow, (short) 15, mbean.getIs_normal().equals("0") ? "否" : "是");
                    createCell(tWb, tRow, (short) 16, mbean.getIs_buy().equals("0") ? "否" : "是");
                    createCell(tWb, tRow, (short) 17, mbean.getWarehouse_name());
                }
            } else {
                createCell(tWb, tRow, (short) 0, "种类编码", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 1, "集中编码", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 2, "产品名称", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 3, "价格(元)", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 4, "规格", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 5, "型号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 6, "质保期", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 7, "单位", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 8, "品牌", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 9, "生产厂商", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 10, "产地", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 11, "供应商", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 12, "图件号", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 13, "结算模式", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 14, "是否正式产品", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 15, "是否采购", HSSFCellStyle.ALIGN_CENTER);
                createCell(tWb, tRow, (short) 16, "所属仓库", HSSFCellStyle.ALIGN_CENTER);
                if(isTel){
                	 createCell(tWb, tRow, (short) 17, "进价", HSSFCellStyle.ALIGN_CENTER);
                }
                for (int i = 0; i < aList.size(); i++) {
                    tRow = tSheet.createRow((short) (i + 1));
                    GSManufactureBean mbean = (GSManufactureBean) aList.get(i);
                    createCell(tWb, tRow, (short) 0, mbean.getGs_manufacture_category_id());
                    createCell(tWb, tRow, (short) 1, mbean.getGs_concentricity());
                    createCell(tWb, tRow, (short) 2, mbean.getGs_manufacture_name());
                    createCell(tWb, tRow, (short) 3, StringUtils.format(mbean.getGs_manufacture_unitprice(), 4));
                    createCell(tWb, tRow, (short) 4, mbean.getGs_manufacture_spec());
                    createCell(tWb, tRow, (short) 5, mbean.getGs_manufacture_type());
                    createCell(tWb, tRow, (short) 6, mbean.getGs_quality_assure_period());
                    createCell(tWb, tRow, (short) 7, mbean.getGs_quant_unit());
                    createCell(tWb, tRow, (short) 8, mbean.getGs_manufacture_brand());
                    createCell(tWb, tRow, (short) 9, mbean.getGs_manufacturer_name());
                    createCell(tWb, tRow, (short) 10, mbean.getGs_make_area());
                    createCell(tWb, tRow, (short) 11, GSBaseManager.getProviderShortName(mbean.getGs_provider_id()));
                    createCell(tWb, tRow, (short) 12, mbean.getGs_drawing_peice_code());
                    createCell(tWb, tRow, (short) 13, DefineDic.getBalanceMode(mbean.getGs_balance_mode()));
                    createCell(tWb, tRow, (short) 14, mbean.getIs_normal().equals("0") ? "否" : "是");
                    createCell(tWb, tRow, (short) 15, mbean.getIs_buy().equals("0") ? "否" : "是");
                    createCell(tWb, tRow, (short) 16, mbean.getWarehouse_name());
                    if(isTel){
                    	createCell(tWb, tRow, (short) 17, ""+mbean.getPlan_price());
                    }
                }
            }
            tWb.write(tOutputStream);
            tOutputStream.flush();
        } catch (Exception ex) {
            GSLogWriter.error(ex.getMessage());
        }
    }

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
    }

    private void createCell(HSSFWorkbook aWb, HSSFRow aRow, short column, String aValue, short aAlign) {
        HSSFCell tCell = aRow.createCell(column);
        tCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        tCell.setCellValue(aValue);
        HSSFCellStyle tCellStyle = aWb.createCellStyle();
        tCellStyle.setAlignment(aAlign);
        tCell.setCellStyle(tCellStyle);
    }

}
